//
//  HJStatusOriginalView.swift
//  zhangmiSinweibo
//
//  Created by zhangmi on 16/5/27.
//  Copyright © 2016年 Paramount Pictures. All rights reserved.
//   原创微博视图

import UIKit
import SnapKit


//设置对齐的边距
let StatusCellMargin: CGFloat = 8
class HJStatusOriginalView: UIView {
    
    var bottomCons: Constraint?
    var statusViewModel: HJStatusViewModel? {
        didSet {
            //数据绑定
            //let url = NSURL(string: statusViewModel?.status?.user?.profile_image_url ?? "")
            headIcon.sd_setImageWithURL(statusViewModel?.headImageURL)
            nameLabel.text = statusViewModel?.status?.user?.name
            //微博时间 需要处理数据
            timeLabel.text = statusViewModel?.time

            
            //微博来源 需要数据处理
            sourceLabel.text = statusViewModel?.sourceText

            contentLabel.text = statusViewModel?.status?.text
            
            //认证图片
            verifiedImageView.image = statusViewModel?.veritiedImage
            mbrankImageView.image = statusViewModel?.mbrankImage

        //给配图视图设置数据源
            //pictureView.pictures = statusViewModel?.status?.pic_urls
            //在设置数据源之前根据图片的数量 来判断是否需要调整 '自己的底部'约束
            //在更新约束之前需要卸载之前记录的约束
            bottomCons?.uninstall()
            if let urls = statusViewModel?.status?.pic_urls where urls.count > 0 {
                //有配图的原创微博
                //设置数据
                pictureView.pictures = urls
                //修改底部约束
                self.snp_updateConstraints(closure: { (make) in
                    self.bottomCons = make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin).constraint
                })
                //显示配图视图
                pictureView.hidden = false
            } else {
                //没有配图的原创微博
                
                //设置数据
                pictureView.pictures = nil
                //修改约束
                self.snp_updateConstraints(closure: { (make) in
                    self.bottomCons = make.bottom.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin).constraint
                })
                //隐藏配图视图
                pictureView.hidden = true
            }

        }
    }
    
    
        override init(frame: CGRect) {
            super.init(frame: frame)
//          backgroundColor = UIColor.yellowColor()
            setupUI()
        }
        
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
        //设置UI界面
        private func setupUI() {
            //手写代码创建的视图的背景色 是头透明的
            sepView.backgroundColor = UIColor.redColor()
            addSubview(sepView)

            addSubview(headIcon)
            addSubview(verifiedImageView)
            addSubview(nameLabel)
            addSubview(mbrankImageView)
            addSubview(timeLabel)
            addSubview(sourceLabel)
            addSubview(contentLabel)
            
            addSubview(pictureView)
            
            //设置最大布局宽度
            //        contentLabel.preferredMaxLayoutWidth = ScreenWidth - 2 * StatusCellMargin
            //设置约束
            
            
            
            sepView.snp_makeConstraints { (make) in
                make.top.left.right.equalTo(self)
                //设置高度
                make.height.equalTo(StatusCellMargin)
            }

            headIcon.snp_makeConstraints { (make) in
                make.top.equalTo(sepView.snp_bottom).offset(StatusCellMargin)
                make.left.equalTo(self).offset(StatusCellMargin)

                make.height.equalTo(40)
                make.width.equalTo(40)
            }
            
            verifiedImageView.snp_makeConstraints { (make) in
                make.right.bottom.equalTo(headIcon)
            }
            nameLabel.snp_makeConstraints { (make) in
                make.top.equalTo(headIcon.snp_top)
                make.left.equalTo(headIcon.snp_right).offset(StatusCellMargin)
            }
            
            mbrankImageView.snp_makeConstraints { (make) in
                make.top.equalTo(headIcon.snp_top)
                make.left.equalTo(nameLabel.snp_right).offset(StatusCellMargin)
            }
            timeLabel.snp_makeConstraints { (make) in
                make.left.equalTo(headIcon.snp_right).offset(StatusCellMargin)
                make.bottom.equalTo(headIcon.snp_bottom)
            }
            sourceLabel.snp_makeConstraints { (make) in
                make.left.equalTo(timeLabel.snp_right).offset(StatusCellMargin)
                make.bottom.equalTo(headIcon.snp_bottom)
            }
            
            contentLabel.snp_makeConstraints { (make) in
                make.top.equalTo(headIcon.snp_bottom).offset(StatusCellMargin)
                make.left.equalTo(headIcon.snp_left)
            }
            
            pictureView.snp_makeConstraints { (make) in
                make.top.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin)
                make.left.equalTo(contentLabel.snp_left)
                //设置测试大小
                make.size.equalTo(CGSize(width: 100, height: 100))
            }
            

            //给原创微博底部添加约束
            self.snp_makeConstraints { (make) in
            make.bottom.equalTo(pictureView.snp_bottom).offset(StatusCellMargin)



            }
    }
        
        //懒加载
        //分隔条
        private lazy var sepView: UIView = UIView()

        private lazy var headIcon: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        //认证的头像
        private lazy var verifiedImageView: UIImageView = UIImageView(image: UIImage(named: "avatar_vip"))
        private lazy var mbrankImageView: UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
        private lazy var timeLabel: UILabel = UILabel(title: "11:11", font: 10, textColor: UIColor.orangeColor())
        private lazy var sourceLabel: UILabel = UILabel(title: "王大锤", font: 10, textColor: UIColor.darkGrayColor())
        private lazy var nameLabel: UILabel = UILabel(title: "马云", font: 14, textColor: UIColor.darkGrayColor())
        private lazy var contentLabel: UILabel = UILabel(title: "我是王大锤,我为自己带盐,我是王大锤,我为自己带盐我是王大锤,我为自己带盐我是王大锤,我为自己带盐我是王大锤,我为自己带盐我是王大锤,我为自己带盐我是王大锤,我为自己带盐我是王大锤,我为自己带盐", font: 14, textColor: UIColor.darkGrayColor(), alignment: .Left)
        //配图视图
        private lazy var pictureView: HJStatusPictureView = HJStatusPictureView()

    }

